﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.IO.IsolatedStorage;

namespace Transportr
{
    public class FileStorage
    {
        private static IsolatedStorageFile f;
        public static bool Valid { get; private set; }
        public static int FileAccessCounter { get; private set; }

        public static void Initialize()
        {
            f = IsolatedStorageFile.GetUserStoreForApplication();
            Valid = true;
        }

        #region File Manipulation

        public static FileStream GetFileStream(string path, FileMode mode)
        {
            if (Valid && (f.FileExists(path) || mode == FileMode.Create))
            {
                FileAccessCounter++;
                return f.OpenFile(path, mode);
            }
            return null;
        }

        public static bool FileExists(string path)
        {
            return Valid && f.FileExists(path);
        }

        public static bool DeleteFile(string path)
        {
            if (Valid && f.FileExists(path))
            {
                f.DeleteFile(path);
                return true;
            }
            return false;
        }

        #endregion

        # region Directory manipulation

        public static bool DirectoryExists(string path)
        {
            return Valid && f.DirectoryExists(path);
        }

        public static void CreateDirectory(string path)
        {
            if(Valid)
                f.CreateDirectory(path);
        }

        public static void DeleteDirectory(string path)
        {
            if (Valid)
                f.DeleteDirectory(path);
        }

        #endregion
    }
}
